<?php
/**
 * 分页适配器
 * @author Yuwei(yuwei1122@gmail.com)
 *
 */
class BluePhp_Library_Page_Adapter
{
	protected $_url; 
	protected $_page; 
	protected $_thestr; 
	protected $_backstr; 
	protected $_nextstr; 
	protected $_pg; 
	protected $_style; 
	protected $_count;
	protected $_size;
	
	public $limit;
	
	/**
	 * 构造函数
	 * @param unknown_type $count
	 * @param unknown_type $size
	 * @param unknown_type $style
	 */
	public function __construct($count,$size = '',$style='page')
	{
		$this->_count = $count;
		$this->_size = $size?$size:PAGE_SIZE;
		$this->_style = $style; 
		$this->_page = ceil($this->_count / $this->_size);
		$this->_pg = isset($_GET['pg'])?$_GET['pg']:1; 
		if (!preg_match('/^[1-9][0-9]*$/',$this->_pg) || empty($this->_pg))
			$this->_pg = 1; 
		if ($this->_pg > $this->_page)
			$this->_pg = $this->_page; 
		/**
		 * limit
		 * @var unknown_type
		 */
		$this->limit = array(($this->_pg - 1) * $this->_size,$this->_size);
		$this->_url = $this->_getUrl(); 
		if(isset($_GET['pg']) && $_GET['pg'] != $this->_pg)
		{ 
			$this->_url = str_replace('?pg='.$_GET['pg'],'?pg='.$this->_pg,$this->_url); 
			$this->_url = str_replace('&pg='.$_GET['pg'],'&pg='.$this->_pg,$this->_url); 
		} 
		if ($this->_page <= 10)
			for ($i = 1;$i < $this->_page + 1;$i++)
				$this->_thestr .= $this->_makepg($i,$this->_pg); 
		else
		{ 
			if ($this->_pg <= 5)
				for ($i = 1;$i < 10;$i++)
					$this->_thestr .= $this->_makepg($i,$this->_pg); 
			else
			{ 
				if (6 + $this->_pg <= $this->_page)
					for ($i = $this->_pg - 4;$i < $this->_pg + 6;$i++)
						$this->_thestr .= $this->_makepg($i,$this->_pg); 
				else
					for ($i= $this->_pg - 4;$i < $this->_page + 1;$i++)
						$this->_thestr .= $this->_makepg($i,$this->_pg); 
			} 
		} 
		$this->_backstr = $this->_gotoback($this->_pg); 
		$this->_nextstr = $this->_gotonext($this->_pg,$this->_page); 
	} 
	
	/**
	 * 分页显示
	 */
	public function show($template = '')
	{
		$page = '共'.$this->_count.'条,每页'.$this->_size.'条,共'.$this->_page.'页&nbsp;'.$this->_backstr.$this->_thestr.$this->_nextstr;
		if($template)
			$template->assign('page', $page);
		return $page;
	}
	
	/**
	 * 生成数字分页
	 * @param unknown_type $i
	 * @param unknown_type $pg
	 */
	protected function _makepg($i,$pg)
	{ 
		if ($i == $pg)
			return '&nbsp;<font class="'.$this->_style.'">'.$i.'</font>'; 
		else
			return '&nbsp;<a href="'.$this->_replacepg($this->_url,5,$i).'" class="'.$this->_style.'"><u>'.$i.'</u></a>'; 
	} 
	
	/**
	 * 生成上一页等信息
	 * @param unknown_type $pg
	 */
	protected function _gotoback($pg)
	{ 
		if ($pg - 1 > 0)
			return $this->_gotoback = '<a href="'.$this->_replacepg($this->_url,3,0).'" class="'.$this->_style.'">首页</a>&nbsp;<a href="'.$this->_replacepg($this->_url,2,0).'" class="'.$this->_style.'">上一页</a>'; 
		else
			return $this->_gotoback = '<span class="'.$this->_style.'">首页&nbsp;上一页</span>'; 
	} 
	
	/**
	 * 生成下一页等信息
	 * @param unknown_type $pg
	 * @param unknown_type $page
	 */
	protected function _gotonext($pg,$page)
	{ 
		if ($pg < $page)
			return '&nbsp;<a href="'.$this->_replacepg($this->_url,1,0).'" class="'.$this->_style.'">下一页</a>&nbsp;<a href="'.$this->_replacepg($this->_url,4,0).'" class="'.$this->_style.'">尾页</a>'; 
		else
			return '&nbsp;<span class="'.$this->_style.'">下一页&nbsp;尾页</span>'; 
	} 
	
	/**
	 * 处理url中$pg的方法,用于自动生成pg=x
	 * @param unknown_type $url
	 * @param unknown_type $flag
	 * @param unknown_type $i
	 */
	protected function _replacepg($url,$flag,$i)
	{ 
		if ($flag == 1)
		{ 
			$temp_pg = $this->_pg; 
			return str_replace('pg='.$temp_pg,'pg='.($this->_pg + 1),$url); 
		}
		else if($flag == 2) 
		{ 
			$temp_pg = $this->_pg; 
			return str_replace('pg='.$temp_pg,'pg='.($this->_pg - 1),$url); 
		}
		else if($flag == 3) 
		{ 
			$temp_pg = $this->_pg; 
			return str_replace('pg='.$temp_pg,'pg=1',$url); 
		}
		else if($flag == 4)
		{ 
			$temp_pg = $this->_pg; 
			return str_replace('pg='.$temp_pg,'pg='.$this->_page,$url); 
		}
		else if($flag == 5)
		{ 
			$temp_pg = $this->_pg; 
			return str_replace('pg='.$temp_pg,'pg='.$i,$url); 
		}
		else
			return $url; 
	} 
	
	/**
	 * 获得当前URL
	 */
	protected function _getUrl()
	{ 
		$url = 'http://'.$_SERVER['HTTP_HOST']; 
		if(isset($_SERVER['REQUEST_URI']))
			$url .= $_SERVER['REQUEST_URI']; 
		else
		{ 
			$url .= $_SERVER['PHP_SELF']; 
			if(!empty($_SERVER['QUERY_STRING']))
				$url .= '?'.$_SERVER['QUERY_STRING']; 
		} 
		//在当前的URL里加入pg=x字样 
		if (!preg_match('(pg=|PG=|pG=|Pg=)', $url))
		{ 
			if (!strpos($url,'?'))
				$url = $url.'?pg=1'; 
			else
				$url = $url.'&pg=1'; 
		} 
		return $url; 
	}
}
?>